﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;

namespace School.Teaching
{
    public static class Teaching
    {

        private static SchoolEntities db = new SchoolEntities();
        public static bool Add_Subject(int Grad_Id, string Subject_Name, string Description)
        {
            try
            {
                TE_Subject Te = new TE_Subject();
                Te.Grad_Id = Grad_Id;
                Te.Subject_Name = Subject_Name;
                Te.Description = Description;
                db.AddToTE_Subject(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static object Get_Subject_By_Grad(int Grad_id)
        {

            return (from a in db.TE_Subject
                    where a.Grad_Id == Grad_id
                    select new { Grad_name = a.ORG_Grade.Name, Sub_name = a.Subject_Name, a.Description, a.Id }
                 );

        }


        public static object Get_Class_By_Grad(int Grad_id)
        {

            return (from a in db.ORG_Class
                    where a.Grade_Id == Grad_id
                    select new { a.Name, a.Id }
                 );

        }
        public static bool DeleteSubject(int Id)
        {
            var deletequery = (from b in db.TE_Subject
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }
        public static bool UpdateSub(int Id, string Subject_Name, string Description)
        {
            var Update = (from b in db.TE_Subject
                          where b.Id == Id
                          select b).Single();
            Update.Subject_Name = Subject_Name;
            Update.Description = Description;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Add_Course(int Subject_Id, string Name, string Description)
        {
            try
            {
                TE_Course Te = new TE_Course();
                Te.Subject_Id = Subject_Id;
                Te.Name = Name;
                Te.Description = Description;
                db.AddToTE_Course(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }

        }
        public static object Get_Course_By_Subject(int Sub_Id)
        {

            return (from a in db.TE_Course
                    where a.Subject_Id == Sub_Id
                    select new { Grad_name = a.TE_Subject.ORG_Grade.Name, Sub_name = a.TE_Subject.Subject_Name, Course_Name = a.Name, a.Description, a.Id }
                 );

        }
        public static bool DeleteCourse(int Id)
        {
            var deletequery = (from b in db.TE_Course
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }
        public static bool UpdateCourse(int Id, string Name, string Description)
        {
            var Update = (from b in db.TE_Course
                          where b.Id == Id
                          select b).Single();
            Update.Name = Name;
            Update.Description = Description;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Add_Ques_Category(string Name)
        {
            try
            {
                TE_Question_Categories Te = new TE_Question_Categories();
                Te.TypeName = Name;
                db.AddToTE_Question_Categories(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }

        }
        public static bool UpdateQues_Catego(int Id, string Name)
        {
            var Update = (from b in db.TE_Question_Categories
                          where b.Id == Id
                          select b).Single();
            Update.TypeName = Name;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteQues_Catego(int Id)
        {
            var deletequery = (from b in db.TE_Question_Categories
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public static IEnumerable<TE_Question_Categories> Get_Question_Categories()
        {

            return (from a in db.TE_Question_Categories
                    select a
                 );
        }
        //Bank Type
        public static bool Add_Ques_Type(string Name)
        {
            try
            {
                TE_Question_Bank_Type Te = new TE_Question_Bank_Type();
                Te.Name = Name;
                db.AddToTE_Question_Bank_Type(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }

        }
        public static bool UpdateQues_Type(int Id, string Name)
        {
            var Update = (from b in db.TE_Question_Bank_Type
                          where b.Id == Id
                          select b).Single();
            Update.Name = Name;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteQues_Bank_Type(int Id)
        {
            var deletequery = (from b in db.TE_Question_Bank_Type
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }
        public static IEnumerable<TE_Question_Bank_Type> Get_Question_Bank_Type()
        {

            return (from a in db.TE_Question_Bank_Type
                    select a
                 );

        }

        //Bank Question
        public static bool Add_Bank_Question(int Certificate_Id, int Stage_Id, int Grade_Id, int Subject_Id, string Question, int QestionType_Id, int Quesion_Bank_Type_Id)
        {
            try
            {
                TE_Question_Bank Te = new TE_Question_Bank();
                Te.Subject_Id = Subject_Id;
                Te.Certificate_Id = Certificate_Id;
                Te.Grade_Id = Grade_Id;
                Te.Stage_Id = Stage_Id;
                Te.Question = Question;
                Te.QestionType_Id = QestionType_Id;
                Te.Quesion_Bank_Type_Id = Quesion_Bank_Type_Id;
                db.AddToTE_Question_Bank(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }

        }
        public static object Get_Bank_Question_BySubj(int Sub_Id)
        {

            return (from a in db.TE_Question_Bank
                    where a.Subject_Id == Sub_Id
                    select new { Grad_name = a.TE_Subject.ORG_Grade.Name, Sub_name = a.TE_Subject.Subject_Name, Question = a.Question, Question_Type = a.TE_Question_Bank_Type.Name, Question_Categ = a.TE_Question_Categories.TypeName, a.Id, Question_id = a.TE_Question_Bank_Type.Id }
                 );

        }


        public static long Get_Type_Bank_Id(int Id)
        {

            var q = from a in db.TE_Question_Bank
                    where a.Id == Id
                    select a;

            long typ_id = q.FirstOrDefault().Quesion_Bank_Type_Id;
            return typ_id;


        }



        public static bool DeleteBank_Question(int Id)
        {
            var deletequery = (from b in db.TE_Question_Bank
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }
        public static bool UpdateBank_Question(int Id, string Question)
        {
            var Update = (from b in db.TE_Question_Bank
                          where b.Id == Id
                          select b).Single();
            Update.Question = Question;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //Bank Answer


        public static bool Add_Bank_Answer(int Question_Id, string Choices)
        {
            try
            {
                TE_Question_Bank_answer Te = new TE_Question_Bank_answer();
                Te.Question_Id = Question_Id;
                Te.Choices = Choices;
                Te.Choice_Type = 2;
                db.AddToTE_Question_Bank_answer(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public static bool Add_Bank_Answer(int Question_Id, string Choices, int Choice_Type)
        {
            try
            {
                TE_Question_Bank_answer Te = new TE_Question_Bank_answer();
                Te.Question_Id = Question_Id;
                Te.Choices = Choices;
                Te.Choice_Type = Choice_Type;
                db.AddToTE_Question_Bank_answer(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static IEnumerable<TE_Question_Bank_answer> Get_Bank_Answer_ByQues_Id(int Ques_Id)
        {

            return (from a in db.TE_Question_Bank_answer
                    where a.Question_Id == Ques_Id
                    select a
                 );

        }
        public static bool DeleteBank_Answer(int Id)
        {
            var deletequery = (from b in db.TE_Question_Bank_answer
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }

        public static bool DeleteBank_Answer_T_F(int Question_ID)
        {
            var deletequery = (from b in db.TE_Question_Bank_answer
                               where b.Question_Id == Question_ID
                               select b).FirstOrDefault();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }


        public static bool UpdateBank_Answer(int Id, string Choice)
        {
            var Update = (from b in db.TE_Question_Bank_answer
                          where b.Id == Id
                          select b).Single();

            Update.Choices = Choice;

            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static int Get_type_answer(int Ques_Id, int id)
        {

            var x = (from a in db.TE_Question_Bank_answer
                     where a.Question_Id == Ques_Id && a.Id == id
                     select a.Choice_Type)
                 .FirstOrDefault().ToString();
            return Convert.ToInt32(x);
        }
        public static bool UpdateBank_Answer_Choice_Type(int Id, int Choice_Type)
        {
            var Update = (from b in db.TE_Question_Bank_answer
                          where b.Id == Id
                          select b).Single();

            Update.Choice_Type = Choice_Type;

            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        //Topic

        public static long Add_Topic(int Course_Id, string Name, string contents)
        {
            long id = 0;
            try
            {
                TE_Topic Te = new TE_Topic();
                Te.Course_Id = Course_Id;
                Te.Name = Name;
                Te.contents = contents;
                db.AddToTE_Topic(Te);
                db.SaveChanges();
                id = Te.Id;
                return id;
            }
            catch (Exception)
            {
                return id = 0;

            }

        }


        public static bool Add_Topic_Classes(int Topic_Id, int Class_Id)
        {

            try
            {
                TE_Topic_Classes Te = new TE_Topic_Classes();
                Te.Topic_Id = Topic_Id;
                Te.Class_Id = Class_Id;
                db.AddToTE_Topic_Classes(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteTopic(int Id)
        {
            var deletequery = (from b in db.TE_Topic
                               where b.Id == Id
                               select b).Single();



            try
            {

                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public static IEnumerable<TE_Topic_Attach> Get_Topic_Attach(int Topic_Id)
        {

            return (from a in db.TE_Topic_Attach
                    where a.Topic_Id == Topic_Id
                    select a
                 );

        }
        public static bool DeleteTopic_Attach(int Id)
        {
            var deletequery = (from b in db.TE_Topic_Attach
                               where b.Id == Id
                               select b).FirstOrDefault();

            try
            {

                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Add_Topic_Attach(int Topic_Id, string Attach)
        {
            try
            {
                TE_Topic_Attach Te = new TE_Topic_Attach();
                Te.Topic_Id = Topic_Id;
                Te.Attach = Attach;
                db.AddToTE_Topic_Attach(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static object Get_Topic_ByCourse(int Cours_Id)
        {

            return (from a in db.TE_Topic
                    where a.Course_Id == Cours_Id
                    select new { Grad_name = a.TE_Course.TE_Subject.ORG_Grade.Name, Sub_name = a.TE_Course.TE_Subject.Subject_Name, Course_Name = a.TE_Course.Name, a.Name, a.Id }
                 );
        }
        public static IEnumerable<TE_Topic> Get_Topic_ByTopic(int Topic_id)
        {
            return (from a in db.TE_Topic
                    where a.Id == Topic_id
                    select a
                 );

        }
        public static bool UpdateTopic(int T_Id, int Course_Id, string Name, string contents)
        {
            var Update = (from b in db.TE_Topic
                          where b.Id == T_Id
                          select b).Single();

            Update.contents = contents;
            Update.Course_Id = Course_Id;
            Update.Name = Name;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //Topic_Question
        public static bool Add_Topic_Question(int Question_Id, int Topic_Id, decimal Degree)
        {
            try
            {
                TE_Topic_Question Te = new TE_Topic_Question();
                Te.Degree = Degree;
                Te.Question_Id = Question_Id;
                Te.Topic_Id = Topic_Id;
                db.AddToTE_Topic_Question(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public static bool DeleteTopic_Question(int Id)
        {
            var deletequery = (from b in db.TE_Topic_Question
                               where b.Id == Id
                               select b).Single();

            try
            {

                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }

        public static object Get_Bank_Question_BySubj(int Sub_Id, int Typ_id)
        {

            return (from a in db.TE_Question_Bank
                    where a.Subject_Id == Sub_Id && a.QestionType_Id == Typ_id && !(db.TE_Topic_Question.Any(b => b.Question_Id == a.Id))
                    select new { Grad_name = a.TE_Subject.ORG_Grade.Name, Sub_name = a.TE_Subject.Subject_Name, Question = a.Question, Question_Type = a.TE_Question_Bank_Type.Name, Question_Categ = a.TE_Question_Categories.TypeName, a.Id }
                 );

        }
        public static object Get_Bank_Question_Homwork_BySubj(int Sub_Id, int Typ_id)
        {

            return (from a in db.TE_Question_Bank
                    where a.Subject_Id == Sub_Id && a.QestionType_Id == Typ_id && !(db.TE_Topic_HomeWork.Any(b => b.Question_Id == a.Id))
                    select new { Grad_name = a.TE_Subject.ORG_Grade.Name, Sub_name = a.TE_Subject.Subject_Name, Question = a.Question, Question_Type = a.TE_Question_Bank_Type.Name, Question_Categ = a.TE_Question_Categories.TypeName, a.Id }
                 );

        }
        public static IEnumerable<TE_Question_Bank> Get_BankQ_ByCat(int su_id, int Cat)
        {

            return (from a in db.TE_Question_Bank
                    where a.QestionType_Id == Cat && a.Subject_Id == su_id && !(db.TE_Topic_Question.Any(b => b.Question_Id == a.Id))
                    select a
                 );
        }
        public static object Get_Topic_Q_ByCat(int Topi_id)
        {

            return (from a in db.TE_Topic_Question
                    where a.Topic_Id == Topi_id
                    select new
                    {
                        Topic_Name = a.TE_Topic.Name,
                        Question_Name = a.TE_Question_Bank.Question,
                        a.Degree,
                        a.Id,
                        a.Question_Id
                    }
                 );
        }
        public static object Get_Topic_Q_inAnswer(int Cour_id)
        {

            return (from a in db.TE_Topic
                    where a.Course_Id == Cour_id //&& !(db.TE_Student_Topic_Answer.Any(b => b.Question_Id == a.Question_Id))
                    select new
                    {
                        Topic_Name = a.Name,
                        a.Id
                    }
                 );
        }
        public static object Get_Topic_Homwork_ByCat(int Topi_id)
        {

            return (from a in db.TE_Topic_HomeWork
                    where a.Topic_Id == Topi_id
                    select new
                    {
                        Topic_Name = a.TE_Topic.Name,
                        Question_Name = a.TE_Question_Bank.Question,
                        a.Degree,
                        a.Id
                    }
                 );

        }
        public static bool Updat_Topic_Q(int id, decimal Degree)
        {
            var Update = (from b in db.TE_Topic_Question
                          where b.Id == id
                          select b).Single();

            Update.Degree = Degree;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //TE_Topic_HomeWork
        public static bool Add_Topic_HomeWork(int Question_Id, int Topic_Id, decimal Degree)
        {
            try
            {
                TE_Topic_HomeWork Te = new TE_Topic_HomeWork();
                Te.Degree = Degree;
                Te.Question_Id = Question_Id;
                Te.Topic_Id = Topic_Id;
                db.AddToTE_Topic_HomeWork(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stud_id"></param>
        /// <returns></returns>
        public static object Get_Subject_ByStud_id(int stud_id)
        {

            return (from c in db.TE_Subject where c.Grad_Id == ((from d in db.TE_Student where d.Id == stud_id select d.Grade_ID).FirstOrDefault()) select c);
        }

        public static IEnumerable<TE_Question_Bank_answer> Get_Question_Bank_answer(int question_Id)
        {

            return (from c in db.TE_Question_Bank_answer where c.Question_Id == question_Id select c);
        }
        public static bool Add_Student_Topic_Answer(int Student_Id, int Question_Id, int Answer_Id)
        {
            try
            {
                TE_Student_Topic_Answer Te = new TE_Student_Topic_Answer();
                Te.Answer_Id = Answer_Id;
                Te.Question_Id = Question_Id;
                Te.Student_Id = Student_Id;
                db.AddToTE_Student_Topic_Answer(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public static bool Delete_Topic_Class(int Id)
        {
            var deletequery = (from b in db.TE_Topic_Classes
                               where b.Topic_Id == Id
                               select b).FirstOrDefault();

            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static int Get_From_Answer(int que_id)
        {
            int count = 0;
            var a = from d in db.TE_Question_Bank_answer
                    where d.Question_Id == que_id
                    select d;
            if (a.Count() > 0)
                count = a.Count();
            return count;
        }
        //
        public static bool Add_TE_Student_Topic_Answer(int Student_Id, int Question_Id, int Answer_Id)
        {
            try
            {
                TE_Student_Topic_Answer Te = new TE_Student_Topic_Answer();
                Te.Student_Id = Student_Id;
                Te.Question_Id = Question_Id;
                Te.Answer_Id = Answer_Id;
                db.AddToTE_Student_Topic_Answer(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
